// comment_provider.dart
import 'package:flutter/material.dart';

import '../model/comment.dart';
import '../utils/api.dart';

class CommentProvider extends ChangeNotifier {
  final int postId;
  List<Comment> _comments = [];
  bool _isLoading = true;

  CommentProvider(this.postId) {
    loadComments();
  }

  List<Comment> get comments => _comments;

  bool get isLoading => _isLoading;

  Future<void> loadComments() async {
    _isLoading = true;
    notifyListeners();

    try {
      final res = await Api.get(
        '/comments/list',
        params: {'postId': postId.toString()},
      );
      if (res['code'] == 0) {
        final List list = res['data'];
        _comments = list.map((e) => Comment.fromJson(e)).toList();
      }
    } catch (e) {
      debugPrint("评论加载失败: $e");
    }

    _isLoading = false;
    notifyListeners();
  }
}
